import EventManager from "../EventManager";
import SceneManager from "../framework/SceneManager";
import StageBase from "../framework/StageBase";

const { ccclass, property } = cc._decorator;

/**
 * AnimationUtils类的辅助类，用于绑定每个动画以提供独立的属性
 */
@ccclass
export default class SimpleAnimateProperties extends cc.Component {

    defaultPos: cc.Vec2 = new cc.Vec2(0, 0);
    defaultScale: cc.Vec2 = new cc.Vec2(1, 1);
    defaultSkew: cc.Vec2 = new cc.Vec2(0, 0);
    times: number;
    n: number;
    positions: cc.Vec2[];
    callback: Function;
    thisObj: any;
    stepNumber: number;
    currStage: StageBase;

    onDestroy() {
        EventManager.removeEvent(SceneManager.Event_StageOut, this.onStageOut, this);
    }

    onLoad() {
        EventManager.addEvent(SceneManager.Event_StageOut, this.onStageOut, this);
    }

    destroyOnStageSwap(b: boolean) {
        this.currStage = b ? SceneManager.currStage : null;
    }

    startMoveByBezier(delay: number) {
        this.unscheduleAllCallbacks();
        this.schedule(() => {
            this.node.position = this.positions[this.n];
            this.n++;
            if (this.n == this.stepNumber) {
                if (this.callback) {
                    this.callback();
                }
            }
        }, 0.01, this.stepNumber - 1, delay);
    }

    zoomTipOnce(delay: number, scaleRatio: number) {
        cc.tween(this.node).delay(delay).to(0.1, { scale: this.defaultScale.x * scaleRatio }).to(0.2, { scale: this.defaultScale.x }).call(() => {
            this.n--;
            if (this.n > 0) {
                this.zoomTipOnce(0, scaleRatio);
            } else {
                if (this.callback) {
                    this.callback();
                }
            }
        }).start();
    }

    onStageOut(stage: StageBase) {
        if (this.currStage == stage && this.node) {
            this.node.destroy();
        }
    }

}